package com.edu.utils;


import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mxs
 * 统一相应结果
 */
@Data
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "响应码")
    private Integer code;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private HashMap<String,Object> data = new HashMap<>();//注意一定要先初始化这个map，否则报错
    //私有空构造方法，防止外部new
    private R(){}

    /**
     * 返回成功
     * @return
     */
    public static R success(){
        R r = new R();
        r.setCode(ResultCode.SUCCESS_CODE);
        r.setMessage("成功");
        r.setSuccess(true);
        return r;
    }
    public static R error(){
        R r = new R();
        r.setCode(ResultCode.FAIL_CODE);
        r.setSuccess(false);
        r.setMessage("失败");
        return r;
    }

    /**
     * set方法全部的返回值都为this，为了便于链式编程
     * 类似于 R.success().message().....
     */

    public R success(Boolean success) {
        this.success = success;
        return this;
    }

    public R code(Integer code) {
        this.code = code;
        return this;
    }

    public R message(String message) {
        this.message = message;
        return this;
    }

    public R data(HashMap<String,Object> data) {
        this.data = data;
        return this;
    }
    public R data(String key,Object data){
        this.data.put(key,data);
        return this;
    }
}
